//
//  STMGoodsManagementTableViewCell.m
//  StoreManagement
//
//  Created by centling on 16/5/20.
//  Copyright © 2016年 alfred. All rights reserved.
//

#import "STMGoodsManagementTableViewCell.h"

@implementation STMGoodsManagementTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setGoodsDetailData:(STMGoodsListDetail *)goodsDetailData {
    if ([self.goodsType isEqualToString:@""]) {
        [self.deleteButton setTitle:@"下架" forState:UIControlStateNormal];
    }
    if ([self.goodsType isEqualToString:@"offline"]) {
        [self.deleteButton setTitle:@"上架" forState:UIControlStateNormal];
    }
    if ([self.goodsType isEqualToString:@"lockup"]) {
        [self.deleteButton setTitle:@"删除" forState:UIControlStateNormal];
    }
    self.commonId = goodsDetailData.goods_commonid;
    [self.goodsImageView sd_setImageWithURL:[NSURL URLWithString:goodsDetailData.goods_image] placeholderImage:[UIImage imageNamed:@"nopic"]];
    self.goodsNameLabel.text = goodsDetailData.goods_name;
    self.goodsPriceLabel.text = goodsDetailData.goods_price;
    self.goodsStorageNumLabel.text = [NSString stringWithFormat:@"库存：%@", goodsDetailData.goods_storage_sum];
    self.goodsSaleNumLabel.text = [NSString stringWithFormat:@"销量：%@", goodsDetailData.goods_salenum];
    [self.deleteButton addTarget:self action:@selector(deleteButtonClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)deleteButtonClick {
    if ([self.goodsType isEqualToString:@""]) {
        [STMNetworkAPI unshowGoodsWithKey:[UserDefaultHandler getUserDetaultString:kUserKey]
                                 commonId:self.commonId
                                  success:^(STMLoginModel *result) {
                                      if ([result.code isEqualToString:@"200"]) {
                                          if (_cellBlock) {
                                              _cellBlock();
                                          }
                                      } else if ([result.code isEqualToString:@"401"]) {
                                          [GlobalUtil presentLoginVC];
                                      } else {
                                          UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                                          [GlobalUtil HUDShowMessage:result.message addedToView:nav.view];
                                      }
                                  }
                                  failure:^(NSError *err) {
                                      UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                                      [GlobalUtil HUDShowMessage:@"请检查网络连接" addedToView:nav.view];
                                  }];
    }
    if ([self.goodsType isEqualToString:@"offline"]) {
        [STMNetworkAPI showGoodsWithKey:[UserDefaultHandler getUserDetaultString:kUserKey]
                               commonId:self.commonId
                                success:^(STMLoginModel *result) {
                                    if ([result.code isEqualToString:@"200"]) {
                                        if (_cellBlock) {
                                            _cellBlock();
                                        }
                                    } else if ([result.code isEqualToString:@"401"]) {
                                        [GlobalUtil presentLoginVC];
                                    } else {
                                        UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                                        [GlobalUtil HUDShowMessage:result.message addedToView:nav.view];
                                    }
                                }
                                failure:^(NSError *err) {
                                    UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                                    [GlobalUtil HUDShowMessage:@"请检查网络连接" addedToView:nav.view];
                                }];
    }

    if ([self.goodsType isEqualToString:@"lockup"]) {
        [STMNetworkAPI deleteGoodsWithKey:[UserDefaultHandler getUserDetaultString:kUserKey]
                                 commonId:self.commonId
                                  success:^(STMLoginModel *result) {
                                      if ([result.code isEqualToString:@"200"]) {
                                          if (_cellBlock) {
                                              _cellBlock();
                                          }
                                      } else if ([result.code isEqualToString:@"401"]) {
                                          [GlobalUtil presentLoginVC];
                                      } else {
                                          UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                                          [GlobalUtil HUDShowMessage:result.message addedToView:nav.view];
                                      }
                                  }
                                  failure:^(NSError *err) {
                                      UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                                      [GlobalUtil HUDShowMessage:@"请检查网络连接" addedToView:nav.view];
                                  }];
    }
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
